<template>
  <view class="page-login-phone">
    <van-cell-group>
			<van-field
				class="mobile"
				:value="form.mobile"
				@input="(e) => {form.mobile = e.detail}"
				clearable
				placeholder="请输入手机号"
			>
			</van-field>
			<van-field
				class="sms"
				:value="form.code"
				@input="(e) => {form.code = e.detail}"
				center
				clearable
				placeholder="请输入短信验证码"
				use-button-slot
			>
				<text slot="button" size="small" type="primary" @tap="getCode"
				>{{code.tip}}</text>
			</van-field>
		</van-cell-group>
    <view class="p-a-15">
			<van-button block round type="info" :loading="loading" @click="submitLogin">登录</van-button>
			<view class="tips">
				<text>未注册的手机号将自动注册立刻运动账号，且表示已阅读并同意 </text>
				<text class="c-blue" @tap="$utils.gotoPage('./agreement')">《立刻用户服务协议》</text>
			</view>
		</view>
  </view>
</template>
<script>
	import login from '@/static/mixins/login';
	export default {
		mixins: [login],
    data(){
      return{
        form: {
					mobile: '',
					code: ''
				},
        code:{
          tip: '获取验证码',
          timer: null,
          time: 0,
        },
        loading: false,
				is_register: false, // 是否去注册
				user_info: null,
				vip_info: null,
      }
    },
		onShow(){
			
		},
    methods:{
      codeCountDown(time) {
				this.code.time = time || 60 //验证码倒计时
				this.code.tip = this.code.time + 's'
				this.code.timer = setInterval(() => {
					this.code.time--
					if(this.code.time > 0){
						this.code.tip = this.code.time + 's'
					}
					else{
						this.code.tip = '获取验证码'
						clearInterval(this.code.timer)
						this.code.timer = null;
					}
				},1000)
			},
      async getCode() {
        if(!this.$verify.mobile(this.form.mobile)){
          return false;
        }
				if(this.code.tip != '获取验证码'){
					return false
				}
				let check_phone = this.checkPhone(this.form.mobile);
				if(!check_phone){
					uni.showModal({
						title: '提示',
						content: '该手机号码未注册，是否需要注册！',
						success: res => {
							if(res.confirm){
								this.getRegisterCode();
								this.codeCountDown();
							}else{
								
							}
						}
					});
					return;
				}else{
					this.getLoginCode();
					this.codeCountDown();
				}
			},
			getLoginCode(){
				this.$http('get', '/nursing-upms/mobile/'+ this.form.mobile, {}, (data,msg) => {
					this.is_register = false;
				});
			},
			getRegisterCode(){
				this.$http('post', '/nursing-admin/scs/account/vip/registcode?wechatBindAble=0', params, (data,msg) => {
					this.is_register = true;
				});
			},
			verifyForm(){
				if(!this.$verify.mobile(this.form.mobile)){
					return false;
				}
				if(!this.$verify.null(this.form.code, '验证码不能为空')){
					return false;
				}
				return true;
			},
      submitLogin(){
				if(!this.verifyForm()){
					return;
				}
        if(this.is_register){
					this.registerByPhoneCode();
				}else{
					this.loginByPhone();
				}				
      },
			loginByPhone(){
				let obj = {
					grant_type: 'mobile',
					mobile: 'SMS@'+ this.form.mobile,
					code: this.form.code
				};
				let str = this.$utils.serialize(obj);
				this.$http('post', '/nursing-auth/mobile/token/sms'+str, {}, data => {
					uni.showToast({title: '登录成功！', icon: 'none'});
					this.user_info = data;
					uni.setStorageSync('access_token', data.access_token);
					if(data){
						this.getVipInfo();
					}
					uni.switchTab({
						url: '/pages/index/index'
					});
				}, msg => {}, {header:{"Authorization":"Basic cGlnOnBpZw==" }}, 'all');
			},
			// 手机和验证码注册账号
			registerByPhoneCode(obj){
				let register_info = {
					phone: this.form.phone,
					userName: 'yjh' + this.$utils.randomStr(9),
					password: this.$utils.randomStr(12),
					passwordConfirm: this.$utils.randomStr(12),
					wechatBindAble: 1,
					code: this.form.code,
				};
				register_info.userName = this.checkUserName(register_info.userName);
				this.$http('post', '/nursing-admin/scs/account/vip/registVip', register_info, (data, msg) => {
					this.loginByPhone();
				})
			},
    }
  }

</script>